package niuke.week01;

import java.util.Scanner;
import java.util.Stack;

/**
 * 给定一个括号字符串str，返回最长的能够完全正确匹配括号字符字串的长度。
 */
public class MaxLengthofKuoHao {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String line = sc.nextLine();
        int[] r1 = method1(line);
        int[] r2 = method2(line);
        for (int i : r1) {
            System.out.print(i+" ");

        }
        System.out.println();
        for (int i : r2) {
            System.out.print(i+" ");
        }
    }

    public static int[] method1(String line) {


        Stack<Integer> s = new Stack<>();
        int [] res = new int[line.length()];

        int max = 0;
        for (int i = 0; i < line.length(); i++) {
            if (line.charAt(i) == '(') {
                s.push(i);
            } else if (line.charAt(i) == ')' && !s.isEmpty()) {
                Integer index = s.pop();
                res[i] = i-index+1 + (index>0?res[index-1]:0);
                max = Math.max( res[i] ,max);
            }
        }
        System.out.println(max);
        return res;
//
//        for (int re : res) {
//            System.out.print(re+" ");
//
//        }
    }

    public static int [] method2(String line) {

        int[] dp = new int[line.length()];//以str[i]结尾的 最长的
        dp[0] = 0;

        int max = 0;
        for (int i = 1; i < dp.length; i++) {
            int j = i - dp[i - 1] - 1;
            if (line.charAt(i) == ')' && j >= 0 && line.charAt(j) == '(') {
                dp[i] = dp[i - 1] + 2 + (j > 0 ? dp[j - 1] : 0);//还要考虑 j 前面是否有连续的
                max = Math.max(dp[i], max);
            }
        }
        System.out.println(max);
        return  dp;
    }
}